package com.atguigu.java2;

import com.atguigu.java1.Person;
import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.stream.Stream;

/**
 * @author : leimingchao
 * @Description:
 * @date Date : 2021年06月13日 18:20
 */
public class OtherTest {

    @Test
    public void test() {
        Class clazz = Person.class;
        //getConstructors():当前运行时类中声明为public的构造器
        Constructor[] constructors = clazz.getConstructors();
        Stream.of(constructors).forEach(System.out::println);
        //getDeclaredConstructors():当前运行时类中所有的构造器
        Stream.of(clazz.getDeclaredConstructors()).forEach(System.out::println);
    }

    /**
     * 获取运行时类的父类
     */
    @Test
    public void test2() {
        Class clazz = Person.class;
        //获取运行时类的父类
        Class superclass = clazz.getSuperclass();
        System.out.println(superclass);
    }

    /**
     * 获取运行时类带泛型的父类
     */
    @Test
    public void test3() {
        Class clazz = Person.class;
        Type genericSuperclass = clazz.getGenericSuperclass();
        System.out.println(genericSuperclass);
    }

    /**
     * 获取运行时类的父类的泛型
     *
     * 代码：逻辑性代码 vs 功能性代码
     */
    @Test
    public void test4() {
        Class clazz = Person.class;
        Type genericSuperclass = clazz.getGenericSuperclass();
        ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
        //获取泛型类型
        Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
        Stream.of(actualTypeArguments).forEach(item ->{
            System.out.println(((Class)item).getName());
        });
    }

    /**
     * 获取运行时类的接口
     */
    @Test
    public void test5() {
        Class clazz = Person.class;
        Stream.of(clazz.getInterfaces()).forEach(System.out::println);
        System.out.println("=======================================");
        //获取运行时类父类的接口
        Stream.of(clazz.getSuperclass().getInterfaces()).forEach(System.out::println);
    }

    /**
     * 获取运行时类的包
     */
    @Test
    public void test6() {
        Class clazz = Person.class;
        Package pack = clazz.getPackage();
        System.out.println(pack);
    }

    /**
     * 获取运行时类的注解
     */
    @Test
    public void test7() {
        Class clazz = Person.class;
        Stream.of(clazz.getAnnotations()).forEach(System.out::println);
    }


}
